package Bill.app.serveur;


import java.io.*;
import java.util.Hashtable;
import java.util.StringTokenizer;

public class Traitement extends Thread
{

    public Traitement(Hashtable LesConnecter, Hashtable tableChat, BufferedReader lecture)
    {
        this.LesConnecter = null;
        this.tableChat = null;
        this.lecture = null;
        this.LesConnecter = LesConnecter;
        this.tableChat = tableChat;
        this.lecture = lecture;
        start();
    }

    public void run()
    {
        do
            try
            {
                String message = lecture.readLine();
                StringTokenizer tt = new StringTokenizer(message, ":");
                String expediteur = "";
                String destinataire = "";
                expediteur = tt.nextToken();
                destinataire = tt.nextToken();
                message = tt.nextToken();
                String key = (new StringBuilder(String.valueOf(destinataire))).append(":").append(expediteur).toString().trim();
                if(message.compareToIgnoreCase("END") == 0)
                {
                    String keyed = (new StringBuilder(String.valueOf(expediteur))).append(":").append(destinataire).toString().trim();
                    tableChat.remove(keyed);
                    stop();
                } else
                if(tableChat.containsKey(key))
                    ((PrintWriter)tableChat.get(key)).println(message);
                else
                    ((PrintWriter)LesConnecter.get(destinataire)).println((new StringBuilder(String.valueOf(expediteur))).append(":").append(message).toString());
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        while(true);
    }

    Hashtable<String, PrintWriter> LesConnecter;
    Hashtable <String, PrintWriter>  tableChat;
    BufferedReader lecture;
}
